package com.mmp.main.servlet.device;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.HospitalDevicePpmDetail;
import com.mmp.main.util.Default;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class PPMVisitProcessServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String deviceId = request.getParameter("deviceId");
		if (deviceId == null || deviceId.isEmpty()) {
			announceErorrMessage(request, response, Default.GENERAL_ERROR_MESSAGE);
			return;
		}

		HospitalDevicePpmDetail ppm;

		String ppmId = request.getParameter("ppmId");
		if (ppmId != null && !ppmId.isEmpty())
			ppm = dbm.findById(Integer.parseInt(ppmId), HospitalDevicePpmDetail.class);
		else
			ppm = new HospitalDevicePpmDetail();

		HospitalDevice device = dbm.findById(Integer.parseInt(deviceId), HospitalDevice.class);
		ppm.setHospitalDevice(device);

		ppm.setVisitMonth(WebUtil.getParamValueAsInteger(request, "visitMonth", 0));
		ppm.setVisitYear(WebUtil.getParamValueAsInteger(request, "visitYear", 0));

		int visitStatusInt = WebUtil.getParamValueAsInteger(request, "visitStatus", 0);
		ppm.setVisitStatus(visitStatusInt);

		if (visitStatusInt == 1 || visitStatusInt == 3) {
			ppm.setVisitDate(WebUtil.getParamValueAsDate(request, "visitDate", null));
			ppm.setVisitCost(WebUtil.getParamValueAsFloat(request, "visitCost", 0));
			ppm.setSubcontractor(WebUtil.getParamValue(request, "subcontractor", null));
			String[] visitReportURL = FileUtil.uploadFiles(request, "visitReportURL");
			if (visitReportURL != null && visitReportURL.length != 0)
				ppm.setVisit_report_URL(Common.concatenateValues(visitReportURL));

			if (visitStatusInt == 3) {
				String delayPenalty = request.getParameter("delayPenalty");
				if (delayPenalty != null && !delayPenalty.isEmpty())
					ppm.setDelayPenalty(Float.parseFloat(delayPenalty));
			}

		} else if (visitStatusInt == 2) {

			ppm.setAbsenceReason(WebUtil.getParamValue(request, "absenceReason", null));

			String[] absenceReportURL = FileUtil.uploadFiles(request, "absenceReportURL");
			if (absenceReportURL != null && absenceReportURL.length != 0)
				ppm.setAbsence_report_URL(Common.concatenateValues(absenceReportURL));

		}

		ppm = dbm.mergeEntity(ppm);

		if (ppm == null)
			setMessage(request, new Message("تمت الاضافة بنجاح", Message.MESSAGE_TYPE.SUCCESS));
		else
			setMessage(request, new Message("تم التعديل بنجاح", Message.MESSAGE_TYPE.SUCCESS));

		sendRedirect(request, response, "SearchForDeviceDisplay?" + Default.SEARCH_DEST_PARAM_NAME + "="
				+ Default.SEARCH_DEST_PPM_VISIT_PARAM_VALUE);

	}
}
